/*
 * Copyright 2016 SoChishun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package m;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 权限规则模型
 *
 * @author SoChishun
 * @version 1.0 2016-5-20
 */
public class RuleModels {

    /**
     * 获取权限节点
     *
     * @return
     * @since 1.0 2016-5-20
     */
    public List<HashMap> getRules() {
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            // 读取jar包含的菜单资源文件路径
            String filepath = Thread.currentThread().getContextClassLoader().getResource("resource/menu.xml").getPath();
            File file = new File(filepath);
            if (!file.exists()) {
                return null;
            }
            document = saxReader.read(new File(filepath));
        } catch (DocumentException ex) {
            Logger.getLogger(RuleModels.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

        // 获取根元素
        Element root = document.getRootElement();
        // 获取所有子元素
        String[] attrnames = new String[]{"code", "name", "text", "class_name", "file_path"};
        String attrvalue;
        HashMap node1, node2, node3;
        List<Element> menus1, menus2, menus3;
        List<HashMap> list1, list2, list3;
        list1 = new ArrayList<HashMap>();
        menus1 = root.elements("menu");
        for (Element el1 : menus1) {
            node1 = new HashMap();
            for (String name : attrnames) {
                attrvalue = el1.attributeValue(name);
                node1.put(name, null == attrvalue ? "" : attrvalue);
            }
            menus2 = el1.elements("menu");
            list2 = new ArrayList<HashMap>();
            for (Element el2 : menus2) {
                node2 = new HashMap();
                for (String name : attrnames) {
                    attrvalue = el2.attributeValue(name);
                    node2.put(name, null == attrvalue ? "" : attrvalue);
                }
                menus3 = el2.elements("menu");
                list3 = new ArrayList<HashMap>();
                for (Element el3 : menus3) {
                    node3 = new HashMap();
                    for (String name : attrnames) {
                        attrvalue = el3.attributeValue(name);
                        node3.put(name, null == attrvalue ? "" : attrvalue);
                    }
                    list3.add(node3);
                }
                node2.put("children", list3);
                list2.add(node2);
            }
            node1.put("children", list2);
            list1.add(node1);
        }
        return list1;
    }

}
